package com.greenleaf.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.greenleaf.entity.SearchCon;
import com.greenleaf.entity.Vip;
import com.greenleaf.service.VipService;

@Controller
@RequestMapping("/vip")
public class VipController {
	@Resource
	private VipService vipService;

	@RequestMapping("/get")
	@ResponseBody
//http://localhost:8080/Greenleaf/vip/get
	public Object getAllVip(SearchCon searchCon) {
		List<Vip> vip = vipService.getAllVip(searchCon);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("code", "0");
		map.put("msg", "0");
		map.put("count", vipService.getAllVip(searchCon));
		map.put("data", vip);
		return map;
	}
	@RequestMapping("/login")
	@ResponseBody
//http://localhost:8080/Greenleaf/vip/login
	public Object login(Vip vip,HttpServletRequest request) {
		
	    //String pwd=StringTools.getMD5(vip.getvPassword());//MD5加密
	  //  vip.setvPassword(pwd);
				try {
					Vip user= vipService.login(vip).get(0);
					if(user==null){
						return false;
					}else{
						HttpSession session=request.getSession();
						session.setAttribute("vipInfo", user);
						session.setMaxInactiveInterval(60*60);//单位为秒
						return true;
					}
				} catch (Exception e) {
					e.printStackTrace();
					return false;
				}
	}

	/**
	 * 退出
	 */
    @RequestMapping("/loginOut")
    @ResponseBody
	public Object loginOut(HttpServletRequest request) {
		try {
				HttpSession session=request.getSession();
				session.invalidate();//销毁session
				return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	@RequestMapping("/add")
	@ResponseBody
//http://localhost:8080/Greenleaf/vip/add
	public Object addVip(Vip vip) {
		Date date = new Date();
		vip.setsTime(date);
		vip.setuTime(date);
		int r=vipService.addVip(vip);
		return r==1?true:false;
	}
	@RequestMapping("/update")
	@ResponseBody
	//http://localhost:8080/Greenleaf/vip/update
	public Object updateVip(Vip vip) {
		int r = vipService.updateVip(vip);
		return r==1?true:false;
	}
	@RequestMapping("/del")
	@ResponseBody
	//http://localhost:8080/Greenleaf/vip/del
	public Object delVip(Integer vipId) {
		int r = vipService.delVip(vipId);
		return r==1?true:false;
	}
}
